layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //品牌列表
    var tableIns = table.render({
        elem: '#merchantList',
        url : '/recommend/template/list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "merchantListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'columnId', title: '推荐栏位', minWidth:100, align:"center"},
            {field: 'num', title: '栏位数量', minWidth:100, align:"center"},
            {field: 'type', title: '返回类型', minWidth:100, align:"center",templet:function(d){
                    return d.type == 1 ? 'json': 'html';
                    }},
            {field: 'createdDate', title: '创建时间', align:'center',minWidth:150,sort:true,
                templet:function(d){
                    //创建时间
                    var date = new Date(d.createdDate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {title: '操作', minWidth:175, templet:'#merchantListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            table.render({
                elem: '#merchantList',
                url : '/merchant/query?name=' + searchKey,
                cellMinWidth : 95,
                page : true,
                height : "full-125",
                limits : [10,15,20,25],
                limit : 20,
                id : "merchantListTable",
                cols : [[
                    {type: "checkbox", fixed:"left", width:50},
                    {field: 'number', title: '推荐规则编号', minWidth:100, align:"center"},
                    {field: 'name', title: '推荐规则名称', minWidth:100, align:"center"},
                    {field: 'pic', title: '推荐规则图片', minWidth:200,style:'height:100px;', align:'center',templet:function(d){
                            if (d.pic != null && d.pic != "") {
                                //处理图片
                                var picArr = d.pic.split(";");
                                return '<img src="'+ picArr[0] +'" width="100" />';
                            }else {
                                return '';
                            }
                        }},
                    {field: 'brief', title: '推荐规则座右铭', align:'center'},
                    {field: 'fansCount', title: '粉丝数', align:'center'},
                    {field: 'manager', title: '掌柜名', align:'center'},
                    {field: 'userId', title: '用户id', align:'center'},
                    {field: 'serverNumber', title: '服务电话', align:'center'},
                    {field: 'addr', title: '所在地', align:'center'},
                    {field: 'merchantStatusId', title: '推荐规则状态id', align:'center'},
                    {field: 'certificateUrl', title: '工商执照链接', align:'center'},
                    {field: 'favorRate', title: '好评率', align:'center'},
                    {field: 'matchRate', title: '推荐规则描述相符度', align:'center'},
                    {field: 'attitueRate', title: '服务态度', align:'center'},
                    {field: 'logisticsRate', title: '物流服务', align:'center'},
                    {field: 'remarks', title: '备注信息', align:'center'},
                    {field: 'createDate', title: '创建时间', align:'center',minWidth:150,sort:true,
                        templet:function(d){
                            //创建时间
                            var date = new Date(d.createDate);
                            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                        }
                    },
                    {field: 'updateDate', title: '更新时间', align:'center',minWidth:150,sort:true,
                        templet:function(d){
                            //更新时间
                            var date = new Date(d.updateDate);
                            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                        }
                    },
                    {title: '操作', minWidth:175, templet:'#merchantListBar',fixed:"right",align:"center"}
                ]]
            });

        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加推荐规则
    function addmerchant(){
        var index = layui.layer.open({
            title : "添加推荐规则",
            type : 2,
            content : "recomTemplate_add.html",
            area: ['100%',$(document).height() + 'px']
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addmerchant();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('merchantListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的推荐规则？', {icon: 3, title: '提示信息'}, function (index) {
                for (var i = 0; i < newsId.length; i++) {
                    $.get("/merchant/delete?id=" + newsId[i], function (data) {
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }


            })
        }else{
            layer.msg("请选择需要删除的推荐规则");
        }
    });

    //列表操作
    table.on('tool(merchantList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var index = layui.layer.open({
                title : "修改推荐规则信息",
                type : 2,
                content : "recomTemplate_edit?id=" + data.id,
                area: ['100%',$(document).height() + 'px'],
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回推荐规则列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此推荐规则？',{icon:3, title:'提示信息'},function(index){
                $.get("/recommend/template/delete?id=" + data.id,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        } else if(layEvent === 'view_default'){ //查看默认商品
            var index = layui.layer.open({
                title : "默认推荐商品",
                type : 2,
                content : "recomTemplate_edit.html?id=" + data.id,
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回推荐规则列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        }
    });

});